package data;

import java.sql.*;
import java.util.*;
import utils.Transacao;

public class SalasData {

  public void incluir(SalasDO sala, Transacao tr) throws Exception {
     Connection con = tr.obterConexao();
     String sql = "insert into Salas (Funcionario_idFuncionario, Tipo, Lotacao, PrecoDiaria) values (?, ?, ?, ?)";
     PreparedStatement ps = con.prepareStatement(sql);
     ps.setInt(1, sala.getIdFuncionario());
     ps.setString(2, sala.getTipo());
     ps.setInt(3, sala.getLotacao());
     ps.setString(4, sala.getPrecoDiaria());
     int result = ps.executeUpdate();
  }

  public void excluir(SalasDO sala, Transacao tr) throws Exception {
     excluir(sala.getIdSalas(), tr);
  } // excluir

  public void excluir (int idSalas, Transacao tr) throws Exception {
     Connection con = tr.obterConexao();
     String sql = "delete from Salas where idSalas=?";
     PreparedStatement ps = con.prepareStatement(sql);
     ps.setInt(1, idSalas);
     int result = ps.executeUpdate();
  } // excluir 

  public void atualizar(SalasDO sala, Transacao tr) throws Exception {
     Connection con = tr.obterConexao();
     String sql = "update Salas set Tipo=?, Lotacao=?, PrecoDiaria where idSalas=?";
     PreparedStatement ps = con.prepareStatement(sql);
     ps.setString(1, sala.getTipo());
     ps.setInt(2, sala.getLotacao());
     ps.setString(3, sala.getPrecoDiaria());
     int result = ps.executeUpdate();
  } // atualizar

  public SalasDO buscar(int idSalas, Transacao tr) throws Exception {
     Connection con = tr.obterConexao();
     String sql = "select * from Salas where  idSalas=?";
     PreparedStatement ps = con.prepareStatement(sql);
     ps.setInt(1, idSalas);
     ResultSet rs = ps.executeQuery();
     rs.next();
     SalasDO sala = new SalasDO();
     sala.setIdSalas (rs.getInt("idSalas"));
     sala.setTipo (rs.getString("Tipo"));
     sala.setLotacao (rs.getInt("Lotacao"));
     sala.setPrecoDiaria(rs.getString("PrecoDiaria"));
     return sala;
  } // buscar

  public Vector pesquisarPorTipo(String Tipo, Transacao tr) throws Exception {
     Connection con = tr.obterConexao();
     String sql = "select * from Salas where Tipo like ?";
     PreparedStatement ps = con.prepareStatement(sql);
     ps.setString(1, Tipo);
     ResultSet rs = ps.executeQuery();
     System.out.println("query executada");
     Vector salas = new Vector();
     while (rs.next()) {
        SalasDO s = new SalasDO();
        s.setIdSalas (rs.getInt("idSalas"));
        s.setTipo (rs.getString("Tipo"));
        s.setLotacao (rs.getInt("Lotacao"));
        s.setPrecoDiaria (rs.getString("PrecoDiaria"));
        System.out.println(" got " + s.getIdSalas());
        salas.add(s);
     }
     return salas;
  } // pesquisarPorTipo

} // SalasData
